<?php
namespace app\controller;

use think\Exception;
use think\facade\Db;
use think\facade\Filesystem;
use think\facade\Request;
use think\facade\View;

class Video extends Base {
    public function info(){
        $videos = Db::table('active_video')->select()->toArray();
        View::assign([
            'videos' => $videos
        ]);
        return View::fetch();
    }


    public function add(){
        if(Request::isPost()){
            try{
                $file = request()->file('video');
                $savename = Filesystem::disk('public')->putFile( '', $file);
                $src = 'http://admin.gxdati.com/storage/'.str_replace("\\","/",$savename);
            }catch(\Exception $e){
                return_json(1,'请上传视频');
            }

            $name = trim(input('post.name'));
            $question = trim(input('post.question'));
            $a = trim(input('post.a',''));
            $b = trim(input('post.b',''));
            $c = trim(input('post.c',''));
            $d = trim(input('post.d',''));
            $right = intval(input('post.right'));
            $status = intval(input('post.status'));

            if(empty($question)){
                return_json(1,'请填写问题');
            }

            $insert = Db::table('active_video')->insert([
                'name'=>$name,
                'src'=>$src,
                'question'=>$question,
                'a'=>$a,
                'b'=>$b,
                'c'=>$c,
                'd'=>$d,
                'right'=>$right,
                'status'=>$status
            ]);

            if(empty($insert)){
                return_json(1,'添加失败,请重试');
            }
            return_json(0,'添加成功');
        }else{
            return View::fetch();
        }
    }

    public function edit(){
        if(Request::isPost()){
            try{
                $file = request()->file('video');
                $savename = Filesystem::disk('public')->putFile( '', $file);
                $src = 'http://admin.gxdati.com/storage/'.str_replace("\\","/",$savename);
            }catch(\Exception $e){
                $src = trim(input('post.src'));
            }

            $id = input('post.id');
            $name = trim(input('post.name'));
            $question = trim(input('post.question'));
            $a = trim(input('post.a',''));
            $b = trim(input('post.b',''));
            $c = trim(input('post.c',''));
            $d = trim(input('post.d',''));
            $right = intval(input('post.right'));
            $status = intval(input('post.status'));

            if(empty($question)){
                return_json(1,'请填写问题');
            }

            $update = Db::table('active_video')->where('id',$id)->update([
                'name'=>$name,
                'src'=>$src,
                'question'=>$question,
                'a'=>$a,
                'b'=>$b,
                'c'=>$c,
                'd'=>$d,
                'right'=>$right,
                'status'=>$status
            ]);
//            if(empty($update)){
//                return_json(1,'修改失败,请重试');
//            }
            return_json(0,'修改成功');
        }else{
            $id = input('get.id');
            if(empty($id)){
                $this->error('请传入视频ID');
            }
            $video = Db::table('active_video')->where('id',$id)->find();
            View::assign([
                'video'=>$video
            ]);
            return View::fetch();
        }
    }

    public function del(){
        $id = input('post.id');
        if(empty($id)){
            return_json(1,'删除失败,请稍后再试');
        }
        $del = Db::table('active_video')->where('id',$id)->delete();
        if(empty($del)){
            return_json(1,'删除失败,请稍后再试');
        }else{
            return_json(0,'删除成功');
        }
    }

    public function upload(){
        $file = request()->file('image');

    }

}